package com.whz.iplocation.ipregion;

import com.whz.iplocation.ipregion.search.ByteIPLocation;
import com.whz.generic.spring.SpringUtils;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;

import java.util.Optional;

/**
 * IP定位服务工具类,默认5天自动更新一次数据源,环境变量:generic.ip.location.auto.update.days=5
 *
 * @author whz
 */
@Slf4j
@UtilityClass
public class IpRegionUtils {

    /**
     * 查询IP地址定位
     *
     * @param ipString 目标IP
     * @return IP地址
     */
    public static String locate(final String ipString) {
        Optional<ByteIPLocation> ip = SpringUtils.getBean(ByteIPLocation.class);
        if (ip.isPresent()) {
            return ip.get().getLocation(ipString).getArea();
        }
        throw new RuntimeException("IP定位服务未启动.");
    }
}